package web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Operator;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import service.PayrollS;
import service.FinanceReportS;

public class FinanceReportW implements Controller {

	private String viewName;
    private int pageSize;
    private FinanceReportS financeReportS;
    private PayrollS payrollS;
    
	public String getViewName() {return viewName;}
	public void setViewName(String viewName) {this.viewName = viewName;}
	public int getPageSize() {return pageSize;}
	public void setPageSize(int pageSize) {this.pageSize = pageSize;}
	public FinanceReportS getFinanceReportS() {return financeReportS;}
	public void setFinanceReportS(FinanceReportS financeReportS) {this.financeReportS = financeReportS;}
	public PayrollS getPayrollS() {return payrollS;}
	public void setPayrollS(PayrollS payrollS) {this.payrollS = payrollS;}

	public ModelAndView handleRequest(HttpServletRequest request,
			                          HttpServletResponse response) throws Exception
    {
		ModelAndView mav=new ModelAndView();
		List model=new ArrayList();
		String dom=request.getParameter("dom");
		 
		Operator operator=(Operator)request.getSession().getAttribute("operator");
		if(null==operator)
		{ 
			mav.setViewName("redirect:/index.htm");
		    return mav;
		}
		if("1011".equals(dom))
		{
			mav.setViewName(viewName+"index");
			
			int bsCount=financeReportS.getBSCount("","");
			int bsPageCount=(bsCount+pageSize-1)/pageSize;
			List bsPage=financeReportS.getBSPage(bsCount,pageSize,1,"","");
			model.add(bsCount);
			model.add(bsPageCount);
			model.add(1);
			model.add(pageSize);
			model.add(bsPage);
			
			int mrCount=financeReportS.getMRCount("","");
			int mrPageCount=(mrCount+pageSize-1)/pageSize;
			List mrPage=financeReportS.getMRPage(mrCount,pageSize,1,"","");
			model.add(mrCount);
			model.add(mrPageCount);
			model.add(1);
			model.add(pageSize);
			model.add(mrPage);
			
			int prCount=payrollS.getCount("","","");
			int prPageCount=(prCount+pageSize-1)/pageSize;
			List prPage=payrollS.getPage(prCount,pageSize,1,"","","");
			model.add(prCount);
			model.add(prPageCount);
			model.add(1);
			model.add(pageSize);
			model.add(prPage);
				  
			mav.addObject("model",model);
		}
		if("4011".equals(dom))
		{
			mav.setViewName(viewName+"bsView");
			String number=new String(request.getParameter("number").getBytes("ISO-8859-1"),"GBK");
			Map bs=financeReportS.getBs(number);
			model.add(bs);
			mav.addObject("model",model);
		}
		if("4012".equals(dom))
		{
			mav.setViewName(viewName+"mrView");
			String number=new String(request.getParameter("number").getBytes("ISO-8859-1"),"GBK");
			Map mr=financeReportS.getMr(number);
			model.add(mr);
			mav.addObject("model",model);
		}
		return mav;
	}
}
